//
//  XQSeventhStepViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/10.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "XQSeventhStepViewController.h"

@interface XQSeventhStepViewController ()

@end

@implementation XQSeventhStepViewController

static const NSString * KEY_STEP  = @"step8";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Do any additional setup after loading the view.
    _currentStep = self.NewRecordArray.firstObject.savedStepCount + 1;
    NSLog(@"setp8 record === %@", self.NewRecordArray.firstObject.getDetailDict);
    [self setNaviBarTitle:self.NewRecordArray.firstObject.name];
    _maintableview=[[UITableView alloc]initWithFrame:CGRectMake(10, 64, fWidth_Current-20, fHeight_Current-64-50)];
    _maintableview.backgroundColor = [UIColor clearColor];
    _maintableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _maintableview.dataSource = self;
    _maintableview.delegate = self;
    _maintableview.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_maintableview];
    
    
    UIImageView*buttomview=[[UIImageView alloc]initWithFrame:CGRectMake(0, fHeight_Current-50, fWidth_Current, 50)];
    buttomview.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
    [self.view addSubview:buttomview];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 50)];
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment=1;
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=[UIColor lightGrayColor];
    label.text= [NSString stringWithFormat:@"当下是第%ld/%ld步",
                 (long)self.NewRecordArray.firstObject.savedStepCount + 1, (long)self.NewRecordArray.firstObject.allStepCount];
    [buttomview addSubview:label];
    jielunarr=[NSMutableArray arrayWithObjects:@"溶血",@"黄疸",@"高脂",@"正常", nil];
    pickerview = [[ZJPickerview alloc] initWithTitle:@"结果选择" KeyName:nil];
    pickerview.zjDelegate = self;
    _iSelectedIndex=0;
    answerArr=[[NSMutableArray alloc]init];
    for (Record *nowRecord in self.NewRecordArray) {
        NSString*timestr=@"";
        if ([[nowRecord getDetailDict] objectForKey:KEY_STEP] &&
            [[[nowRecord getDetailDict] objectForKey:KEY_STEP] count] > 1 &&
            [[[[nowRecord getDetailDict] objectForKey:KEY_STEP] objectAtIndex:1] objectForKey:KEY_RECORD_ANSWER]) {
            
            timestr = [[[[nowRecord getDetailDict] objectForKey:KEY_STEP] objectAtIndex:1] objectForKey:KEY_RECORD_ANSWER];
            
        }
        
        [answerArr addObject:timestr];
    }
}
#pragma mark - UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        return 1+self.NewRecordArray.count;
    }else{
        return 1;

    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;//返回标题数组中元素的个数来确定分区的个数
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        NSString*titlestr=@"8、技术员记录特殊情况。";
        
        CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
        
        return 40+hight.height+150;
        
    }else if(indexPath.section==1){
        if (indexPath.row==0) {
            return 60;
        }else{
            return 90;
        }    }
    else{
        return 50;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView*headview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 18)];
    headview.backgroundColor=[UIColor clearColor];
    return headview;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *strIdentifier = @"strIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
    
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:strIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
        
    }
    switch (indexPath.section) {
        case 0:
            if (indexPath.row==0) {
                NSString*titlestr=@"8.1、技术员记录特殊情况。";
                CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
                UILabel *titlelab=[[UILabel alloc]initWithFrame:CGRectMake(10, 20, fWidth_Current-40, hight.height)];
                titlelab.textColor=[UIColor whiteColor];
                titlelab.backgroundColor=[UIColor clearColor];
                titlelab.text=titlestr;
                titlelab.numberOfLines=0;
                titlelab.font=[UIFont systemFontOfSize:17];
                
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0, hight.height+40, fWidth_Current-20, 0.5)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                _detailView=[[UITextView alloc]initWithFrame:CGRectMake(5, 10+hight.height+40, fWidth_Current-20-10, 130)];
                _detailView.backgroundColor=[UIColor clearColor];
                _detailView.font=[UIFont systemFontOfSize:16];
                _detailView.textColor=[UIColor whiteColor];
                _detailView.delegate=self;
                _detailView.returnKeyType = UIReturnKeyDone;//变为搜索按钮
                if (!tishilab) {
                    tishilab=[[UILabel alloc]initWithFrame:CGRectMake(5, 0, fWidth_Current-20-10, 35)];
                    tishilab.numberOfLines=0;
                    tishilab.text = @"请输入特殊情况！";
                    tishilab.textColor=[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
                    tishilab.font=[UIFont systemFontOfSize:16];
                    [_detailView addSubview:tishilab];
                }
                
                [cell addSubview:_detailView];
                [cell addSubview:titlelab];
                [cell addSubview:lineview];
                
                if ([[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] &&
                    [[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] count] > 0 &&
                    [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER]) {
                    
                    _detailView.text = [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER];
                    tishilab.hidden=YES;
                }
                if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                    _detailView.userInteractionEnabled = NO;
                }else{
                    _detailView.userInteractionEnabled = YES;
                }

                
            }
            break;
        case 1:{
            if (indexPath.row==0) {
                NSString*titlestr=@"8.2、结果。";
                
                UILabel *titlelab=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, fWidth_Current-40, 60)];
                titlelab.textColor=[UIColor whiteColor];
                titlelab.backgroundColor=[UIColor clearColor];
                titlelab.text=titlestr;
                titlelab.numberOfLines=0;
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0,60, fWidth_Current-20, 0.5)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                [cell addSubview:titlelab];
                [cell addSubview:lineview];
            }else {
                Record*nowrecord=[self.NewRecordArray objectAtIndex:indexPath.row-1];
                NSString*answer=[answerArr objectAtIndex:indexPath.row-1];
                UILabel*recordNameLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, fWidth_Current, 30)];
                recordNameLab.backgroundColor=[UIColor clearColor];
                recordNameLab.textColor=[UIColor whiteColor];
                recordNameLab.font=[UIFont systemFontOfSize:16];
                recordNameLab.text=[NSString stringWithFormat:@"样本号：%@",nowrecord.recordId];
                [cell addSubview:recordNameLab];
                
                UILabel*numberLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, fWidth_Current-40-55, 60)];
                numberLabel.textColor=[UIColor whiteColor];
                numberLabel.backgroundColor=[UIColor clearColor];
                
                [cell addSubview:numberLabel];
                UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(fWidth_Current-20-60, 18+30, 50, 25)];
                btn1.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn1.layer.cornerRadius=10;
                btn1.tag=indexPath.row-1;
                [btn1 setTitle:@"选择" forState:UIControlStateNormal];
                [btn1 addTarget:self action:@selector(selectnumber:) forControlEvents:UIControlEventTouchUpInside];
                btn1.titleLabel.font=[UIFont systemFontOfSize:11];
                btn1.clipsToBounds=YES;
                [cell addSubview:btn1];
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0,89, fWidth_Current-20, 0.5)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                [cell addSubview:lineview];
                if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                    btn1.hidden = YES;
                }else{
                    btn1.hidden = NO;
                }
                
                
                numberLabel.text =answer;
                
                
                
            }
        }
            break;
            
        case 2:{
            cell.backgroundColor=[UIColor clearColor];
            if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                
                UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
                btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn2.layer.cornerRadius=5;
                [btn2 setTitle:@"返回列表" forState:UIControlStateNormal];
                btn2.clipsToBounds=YES;
                [btn2 addTarget:self action:@selector(nextStepClick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn2];
            }else{

                UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (fWidth_Current-30)/2, 50)];
                btn1.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
                btn1.layer.cornerRadius=5;
                [btn1 setTitle:@"取消" forState:UIControlStateNormal];
                btn1.clipsToBounds=YES;
                [btn1 addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn1];
                UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake((fWidth_Current-30)/2+10, 0, (fWidth_Current-30)/2, 50)];
                btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn2.layer.cornerRadius=5;
                [btn2 setTitle:@"保存" forState:UIControlStateNormal];
                btn2.clipsToBounds=YES;
                [btn2 addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn2];
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)textViewDidBeginEditing:(UITextView *)textView{
    tishilab.hidden=YES;
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    
    if (textView.text.length == 0) {
        tishilab.hidden=NO;
    }else{
        tishilab.hidden=YES;
    }
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        tishilab.hidden=NO;
    }else{
        tishilab.hidden=YES;
    }
}
-(void)saveClick{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存" message:@"保存之后信息将无法修改" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    alert.delegate=self;
    alert.tag=1;
    [alert show];
    
    
}
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag==1) {
        if (buttonIndex==1) {
            [self creatDetailDict];
 
            UIViewController*miloViewControl=(UIViewController *)[[self.navigationController viewControllers]objectAtIndex:1];
            [self.navigationController popToViewController:miloViewControl animated:YES];
            
            
        }
    }
}
-(void)creatDetailDict{
    if ([_detailView.text isEqualToString:@""]) {
        _detailView.text=@"无";
    }
    NSDictionary *question1Dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"8.1、技术员记录特殊情况", KEY_RECORD_QUESTION,
                                   TYPE_QUESTION_DEFAULT, KEY_RECORD_TYPE,
                                   _detailView.text, KEY_RECORD_ANSWER, nil];
    
    
    for (Record *oneRecord in self.NewRecordArray) {
        NSInteger index =[self.NewRecordArray indexOfObject:oneRecord];
        NSString *timestr=[answerArr objectAtIndex:index];
        NSDictionary *question2Dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                       @"8.2、结果", KEY_RECORD_QUESTION,
                                       TYPE_QUESTION_DEFAULT, KEY_RECORD_TYPE,
                                       timestr, KEY_RECORD_ANSWER, nil];
        NSArray *qustionArr = [NSArray arrayWithObjects:question1Dict,question2Dict, nil];
        
        NSMutableDictionary *tempDict = [oneRecord getDetailDict];
        [tempDict setObject:qustionArr forKey:KEY_STEP];
        [oneRecord setDetailDict:tempDict];
        oneRecord.recordDetail = [tempDict JSONString];
        oneRecord.savedStepCount = 8;
        oneRecord.recordStatus = STATUS_RECORD_FINISHED;
        [oneRecord update];
        
    }
}
-(void)cancelclick{
    UIViewController*miloViewControl=(UIViewController *)[[self.navigationController viewControllers]objectAtIndex:1];
    [self.navigationController popToViewController:miloViewControl animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)nextStepClick{
    
    self.NewRecordArray.firstObject.savedStepCount = _currentStep;

    UIViewController*miloViewControl=(UIViewController *)[[self.navigationController viewControllers]objectAtIndex:1];
    [self.navigationController popToViewController:miloViewControl animated:YES];
}
-(void)selectnumber:(UIButton *)sender{
    [self.view endEditing:YES];
    nowrow=sender.tag;
    if (pickerview.isShowed==NO) {
        [pickerview showInView:self.view withArray:jielunarr selectedIndex:0];
    }
}
#pragma mark - ZJPickerviewDelegate
-(void)ZJPickerview:(ZJPickerview *)zjPickerview didFineshesAtIndex:(int)index{
    if (zjPickerview==pickerview) {
        _iSelectedIndex=index;
        NSString*answer=[NSString stringWithFormat:@"%@",[jielunarr objectAtIndex:index]];
        [answerArr replaceObjectAtIndex:nowrow withObject:answer];
        [_maintableview reloadData];
        
    }
    //    [timebtn setTitle:[timearr objectAtIndex:_iSelectedIndex] forState:UIControlStateNormal];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        [self.view endEditing:YES];
        
        //在这里做你响应return键的代码
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    
    return YES;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
